package com.example.easycharge.repository;

import com.example.easycharge.entity.ChargePile;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface ChargePileRepository extends JpaRepository<ChargePile, Long> {
    ChargePile findChargePileByChargePileId(long pileId);

    @Modifying
    @Transactional
    @Query(value = "update ChargePile pile set pile.active = true where pile.chargePileId=:pileId")
    void setPileActive(long pileId);

    @Modifying
    @Transactional
    @Query(value = "update ChargePile pile set pile.active = false where pile.chargePileId=:pileId")
    void setPileShutOff(long pileId);

    @Modifying
    @Transactional
    @Query(value = "update ChargePile pile set pile.error = true,pile.active=false where pile.chargePileId=:pileId")
    void setPileError(long pileId);

    @Modifying
    @Transactional
    @Query(value = "update ChargePile pile set pile.error = false where pile.chargePileId=:pileId")
    void setPileRecovery(long pileId);

    @Modifying
    @Transactional
    @Query(value = "update ChargePile pile set pile.chargeBill=pile.chargeBill+:chargeBill," +
            "pile.serviceBill=pile.serviceBill+:serviceBill," +
            "pile.totalBill=pile.totalBill+:chargeBill+:serviceBill" +
            " where pile.chargePileId=:pileId")
    void addBill(long pileId, double chargeBill, double serviceBill);

    @Modifying
    @Transactional
    @Query(value = "update ChargePile pile set pile.chargeQuantity=pile.chargeQuantity+:addQuantity where pile.chargePileId=:pileId")
    void addChargeQuantity(long pileId, double addQuantity);

    @Modifying
    @Transactional
    @Query(value = "update ChargePile pile set pile.chargeMinutes=pile.chargeMinutes+1 where pile.chargePileId=:pileId")
    void addChargeMinutes(long pileId);

    int countChargePileByChargePileId(long pileId);

    @Modifying
    @Transactional
    @Query(value = "update ChargePile pile set pile.workMinutes=pile.workMinutes+1 where pile.active=true")
    void addWorkMinutes();

    @Modifying
    @Transactional
    @Query(value = "update ChargePile pile set pile.chargeTime=pile.chargeTime+1 where pile.chargePileId=:pileId")
    void addWorkTimes(long pileId);

    List<ChargePile> findAll();

    Page<ChargePile> findAll(Pageable pageable);

    Page<ChargePile> findAllByActive(Pageable pageable, boolean active);

    Page<ChargePile> findAllByError(Pageable pageable, boolean error);

    Page<ChargePile> findAllByDc(Pageable pageable, boolean dc);


    @Modifying
    @Transactional
    @Query(value = "update ChargePile pile set pile.active = false")
    void cleanPileState();
}
